package cc.messcat.entity.system;

import cc.messcat.service.fupin.FamilyService;
import cc.messcat.service.fupin.PoorFamilyService;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import java.util.Map;

/**
 * 应用相关参数属性类
 */

public class ApplicationProperties {

    private static ServletContext  servletContext = ContextLoader.getCurrentWebApplicationContext().getServletContext();
    private static PoorFamilyService poorFamilyService
            = WebApplicationContextUtils.getWebApplicationContext(servletContext).getBean(PoorFamilyService.class);
    private static FamilyService familyService
            = WebApplicationContextUtils.getWebApplicationContext(servletContext).getBean(FamilyService.class);
    /**
	 * 获取业务年度，以数据备份为节点来确定业务年度
	 * @return 业务年度
	 */
	public static String getAnnual() {
		return servletContext.getAttribute("businessAnnual").toString();
	}

	/**
	 * 获取指定年度指定区域的贫困户数
	 */
	public static String getPoorFamilyCount(String annual,Long areaID) {
        Object poorFamilyCountMap = servletContext.getAttribute("poorFamilyCount");
        Object poorFamilyCount = null;
        if(poorFamilyCountMap != null) {
            poorFamilyCount = ((Map) poorFamilyCountMap).get(annual + areaID);
        }

        return poorFamilyCount == null ? poorFamilyService.getPoorFamilyCount(annual,areaID).toString() : poorFamilyCount.toString();
    }

    /**
     * 获取指定年度指定区域的贫困人口数
     */
    public static String getFamilyCount(String annual,Long areaID) {
        Object familyCountMap = servletContext.getAttribute("familyCount");
        Object familyCount = null;
        if (familyCountMap != null) {
            familyCount = ((Map)familyCountMap).get(annual+areaID);
        }

        return familyCount == null ? familyService.getFamilyCount(annual,areaID).toString() : familyCount.toString();
    }

}
